const appSecret = "938db1545e2e428daf964ec1ac99e21a";
const userId = "1b448be323";
const appId = "frlr1zazn3";
const channelId = "2477096"; //2524683
//生成签名
function getSign(appSecret, params) {
  var newParams = {};
  Object.keys(params)
    .sort()
    .map((key) => {
      newParams[key] = params[key];
    });
  let paramsStr = "";
  for (const key in newParams) {
    paramsStr += key + "" + newParams[key];
  }
  // console.log("paramsStr=" + paramsStr);
  const finalStr = appSecret + paramsStr + appSecret;
  return md5(finalStr).toUpperCase();
}
// 获得聊天室token
function getChatToken() {
  return new Promise((reslove, reject) => {
    const apiUrl = "http://api.polyv.net/live/v3/channel/common/get-chat-token";
    const params = {
      appId: appId,
      channelId: channelId,
      role: "viewer",
      timestamp: new Date().getTime(),
      userId: userId,
    };
    $.ajax({
      url: apiUrl,
      type: "POST",
      data: {
        appId: appId,
        timestamp: params.timestamp,
        sign: getSign(appSecret, params),
        channelId: channelId,
        userId: userId,
        role: params.role,
      },
      success: (data) => {
        reslove(data.data);
      },
    });
  });
}
//获取频道详细基信息
function getChannelInfoDetail() {
  return new Promise((reslove, reject) => {
    const apiUrl = "http://api.polyv.net/live/v3/applet/sdk/get-channel-token-detail";
    const params = {
      appId: appId,
      channelId: channelId,
      timestamp: new Date().getTime(),
    };
    $.ajax({
      url: apiUrl,
      type: "POST",
      data: {
        appId: appId,
        timestamp: params.timestamp,
        sign: getSign(appSecret, params),
        channelId: channelId,
      },
      success: (data) => {
        console.log("频道详情=========")
        console.log(data.data)
        reslove(data.data);
      },
    });
  });
}
//上传图片
function uploadImg(file) {
  return new Promise((reslove, reject) => {
    const apiUrl = "http://api.polyv.net/live/v3/common/upload-image";
    const params = {
      appId: appId,
      type: "menuImage",
      timestamp: new Date().getTime(),
    };
    var myFormData = new FormData();
    myFormData.append("appId", appId);
    myFormData.append("timestamp", params.timestamp);
    myFormData.append("sign", getSign(appSecret, params));
    myFormData.append("file", file);
    myFormData.append("type", "menuImage");
    $.ajax({
      url: apiUrl,
      type: "POST",
      data: myFormData,
      processData: false,
      contentType: false,
      success: (data) => {
        reslove(data.data);
      },
      error:(err)=>{
        console.log(err)
      }
    });
  });
}
//获取当前回放列表
function getCurrentPlayList(){
  return new Promise((reslove, reject) => {
    const apiUrl = `https://api.polyv.net/live/v2/channel/recordFile/${channelId}/jsonp/playback-list?`
    +`callback=jsonp_callback_apipolyvnet_playbacklist&page=1`;
    const params = {
      appId: appId,
      channelId: channelId,
      timestamp: new Date().getTime(),
    };
    $.ajax({
      url: apiUrl,
      dataType: 'jsonp',
      jsonp: 'callback', // 3
      jsonpCallback: 'jsonp_callback_apipolyvnet_playbacklist', // 4
      success: (data) => {
        console.log("当前回放列表=========")
        console.log(data)
        reslove(data);
      },
    });
  });
}
//获取章节
function getChapterList(fileId,type) {
  return new Promise((reslove, reject) => {
    const apiUrl = "http://apichat.polyv.net/front/getPPTRecords";
    const params = {
      id: fileId,
      roomId: channelId,
      type: type,
    };
    $.ajax({
      url: apiUrl,
      data:params,
      success: (data) => {
        console.log("当前章节列表=========")
        console.log(data)
        reslove(data);
      },
    });
  });
}
//获取当前暂存列表
function getRecordList(fileId,type) {
  return new Promise((reslove, reject) => {
    const apiUrl = `http://api.polyv.net/live/v2/channel/recordFile/${channelId}/lastest-video`;
    $.ajax({
      url: apiUrl,
      success: (data) => {
        console.log("当前暂存列表=========")
        console.log(data.data)
        reslove(data.data);
      },
    });
  });
}